import { Body, Controller, Get, Post, Req, Res } from '@nestjs/common';
import { FastifyRequest, FastifyReply } from 'fastify';
import { ModelService } from './model.service';

@Controller('/model')
export class ModelController {


    constructor(private readonly ModelService: ModelService) { }

    /**
     * @description 获取当前应用配置
     * 
     * @memberof ModelController
     */
    @Post('/appconfig')
    async getConfig(@Req() req: FastifyRequest, @Res() res: FastifyReply, @Body() body) {
        return res.code(200).send(this.ModelService.getConfig());
    }

    /**
     * 获取所有应用数据
     *
     * @memberof ModelController
     */
    @Post('/getallapp')
    async getAllApp(@Req() req: FastifyRequest, @Res() res: FastifyReply, @Body() body) {
        return res.code(200).send(this.ModelService.getAllApp());
    }

    /**
     * 设置激活应用
     *
     * @memberof ModelController
     */
    @Post('/setactiveapp')
    async setActiveApp(@Req() req: FastifyRequest, @Res() res: FastifyReply, @Body() body) {
        const { codeName } = body;
        return res.code(200).send(this.ModelService.setActiveApp({ codeName }));
    }

    /**
     * @description 获取模型
     * 
     * @memberof ModelController
     */
    @Get('/*')
    async getModel(@Req() req: FastifyRequest, @Res() res: FastifyReply, @Body() body): Promise<any> {
        const path = `/${req.params['*']}`;
        const result = await this.ModelService.getModel(path);
        return res.code(200).send(result);
    }

    /**
     * @description 更改模型
     * 
     * @memberof ModelController
     */
    @Post('/changemodel')
    async changeModel(@Req() req: FastifyRequest, @Res() res: FastifyReply, @Body() body) {
        const { type, path } = body;
        return res.code(200).send(this.ModelService.changeModel({ type, path }));
    }
}
